<template>
  <div>
    <div class="flexrowbetween flexwrapper" style="align-items: flex-start">
      <div>
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          label-width="0"
        >
          <el-form-item prop="name">
            <el-input style="width:150px"
              v-model="queryParams.name"
              placeholder="请输入活动名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <!-- <el-form-item prop="status1">
            <el-select
              v-model="queryParams.status"
              placeholder="全部活动"
              style="width: 120px"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item> -->
          <!-- <el-form-item prop="day">
            <el-radio-group v-model="queryParams.day">
              <el-radio-button v-for="(item,index) in daylist" :label="item.value" :key="index">{{item.name}}</el-radio-button>
            </el-radio-group>
          </el-form-item> -->
          <el-form-item>
            <el-select
              v-model="timetype"
              style="width: 100px"
            >
              <el-option label="核销时间" :value="1"></el-option>
              <el-option label="中奖时间" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="day">
            <el-date-picker
              v-model="times"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item prop="code">
            <el-input style="width:120px"
              v-model="queryParams.code"
              placeholder="请输入券码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              class="f-14"
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button
              class="f-14"
              type=""
              size="mini"
              :loading='exportloading'
              @click="handleExport"
            >导出</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      ref="tablelayout"
      class="commtable"
    >
      <el-table-column label="平台" prop="platName" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.platName || '-' }} 
        </template>
      </el-table-column>
      <!-- <el-table-column label="活动分类" width="120" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.activeid }} 抽奖</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="活动名称"
        prop="task.name"
        min-width="200"
        align="center"
      />
      <el-table-column
        label="活动编号"
        prop="task_id"
        width="120"
        align="center"
      />
      <el-table-column
        label="核销时间"
        prop="last_time"
        width="260"
        align="center"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.last_time">
            <span v-if="scope.row.address_time">{{ new Date(scope.row.last_time).getTime() > new Date(scope.row.address_time).getTime() ? scope.row.last_time : scope.row.address_time  }}</span>
            <span v-else>{{scope.row.last_time||'-'}}</span>
          </span>
          <span v-else-if="scope.row.address_time">{{scope.row.address_time||'-'}}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        label="中奖时间"
        prop="create_time"
        width="260"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{scope.row.create_time||'-'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="前台核销员" prop="time" width="200" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.member?scope.row.member.user_login:'-'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="后台核销员" prop="time" width="200" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.admin?scope.row.admin.user_login:'-'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="卷码" prop="code" width="200" align="center" />
      <el-table-column
        label="核销方式"
        prop="time"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.draw.draw_type==1">线上</span>
          <span v-if="scope.row.draw.draw_type==2">线下</span>
        </template>
      </el-table-column>
      <el-table-column
        label="奖项等级"
        prop="draw.level_name"
        width="200"
        align="center"
      />
      <el-table-column
        label="奖品名称"
        prop="draw.draw_name"
        width="200"
        align="center"
      />
      <el-table-column label="中奖者" prop="time" width="200" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.wxuser.name || '-'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" width="200" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.more">{{ scope.row.more.beizhu }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.num"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { onlineIndex,onlineIndexExport } from '@/api/hexiao'
export default {
  data() {
    return {
      queryParams: {
        page: 1,
        num: 10,
        name:undefined,
        status: undefined,
        code: undefined
      },
      total: 1,
      timetype:1,
      loading: false,
      tableData: [],
      daylist: [
        { name: "今天", value: 1 },
        { name: "昨天", value: 2 },
        { name: "最近7天", value: 7 },
        { name: "最近30天", value: 30 },
        { name: "本月", value: 31 },
      ],
      times:'',
      exportloading:false
    };
  },

  props: {
    type: {
      type: Number,
      default: 1,
    },
  },

  created() {
    this.getList()
  },

  methods: {
    getList() {
      this.loading = true;
      let params = { 
        page:this.queryParams.page,
        num:this.queryParams.num  
      };
      if(this.queryParams.code){
        params.code = this.queryParams.code
      }
      if(this.queryParams.name){
        params.name = this.queryParams.name
      }
      // if(this.queryParams.status || this.queryParams.status == 0){
      //   params.status = this.queryParams.status
      // }
      // 
      if(this.times && this.times.length){
        if(this.timetype == 1){
          params.last_startime = this.times[0]
          params.last_endtime = this.times[1]
        }else{
          params.create_startime = this.times[0]
          params.create_endtime = this.times[1]
        }
      }
      onlineIndex(params).then((response) => {
        this.tableData = response.data.list;
        this.total = response.data.count;
        this.loading = false;
      });
      this.loading = false;
    },

    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },

    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleExport(){
      let params = {
        head:['平台','活动名称','活动编号','核销时间','中奖时间','后台核销员','前台核销员','券码','核销方式','奖项等级','奖品名称','中奖者','核销状态'],
        keys:['tp_task_name','task.name','task.id','last_time','create_time','admin.user_login','member.user_login','code','draw.draw_type_name','draw.level_name','draw.draw_name','wxuser.name','status_name'],
      }
      if(this.queryParams.code){
        params.code = this.queryParams.code
      }
      if(this.queryParams.name){
        params.name = this.queryParams.name
      }
      if(this.time && this.time.length){
        if(this.timetype == 1){
          params.last_startime = this.time[0]
          params.last_endtime = this.time[1]
        }else{
          params.create_startime = this.time[0]
          params.create_endtime = this.time[1]
        }
      }
      this.exportloading = true
      onlineIndexExport(params).then(res=>{
        let url = res.data
        this.$message.success("正在下载，请稍后");
        var a = document.createElement("a");
        a.setAttribute("href", url);
        a.click();
        this.exportloading = false
      })
    }
  },
};
</script>

<style lang="scss" scoped>
.iconbox {
  width: 32px;
  height: 26px;
}
.imgmini {
  width: 40px;
  height: 40px;
  margin: auto;
}
.imgbox {
  width: 120px;
  height: 120px;
}
</style>
<style lang="scss">
.noheaderdia {
  .el-dialog__header {
    padding: 0 !important;
  }
  .el-dialog__body {
    padding: 0 !important;
  }
}
</style>